package org.ioc.com.book7.controller;

import lombok.extern.slf4j.Slf4j;
import org.ioc.com.book7.model.BookInfo;
import org.ioc.com.book7.model.PageRequest;
import org.ioc.com.book7.model.PageResult;
import org.ioc.com.book7.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookInfoController {
    @Autowired
    private BookInfoService bookInfoService;
    //添加图书
    @RequestMapping("/addBook")
    public String addBook(@RequestBody BookInfo bookInfo){
        log.info("接收到的参数为bookInfo {}",bookInfo);
        //验证参数
        if (!StringUtils.hasLength(bookInfo.getBookName())||
            !StringUtils.hasLength(bookInfo.getPublish())||
            !StringUtils.hasLength(bookInfo.getAuthor())||
            bookInfo.getCount()==null||
            bookInfo.getPrice()==null||
            bookInfo.getStatus()==null

        ){
            return "参数传递错误";
        }
        Integer integer=bookInfoService.insert(bookInfo);
        return "添加图书数量="+integer;
    }
    //查询图书

    @RequestMapping("/selectBook")
    public PageResult<BookInfo> selectBook(PageRequest pageRequest){

        PageResult<BookInfo> page=bookInfoService.select(pageRequest);
        return page;
    }
    /*
    修改图书
    显示要修改的图书信息
    修改图书
     */
    @RequestMapping("/selectOne")
    public BookInfo selectBook(Integer id){
        BookInfo bookInfo=bookInfoService.selectOne(id);
        return bookInfo;
    }

    @RequestMapping("/updateBook")
    public Integer updateBook(BookInfo bookInfo){
        Integer up=bookInfoService.update(bookInfo);
        return up;
    }
    //删除图书
    @RequestMapping("/deleteBook")
    public Integer deleteBook(Integer id){
        Integer delete=bookInfoService.delete(id);
        return delete;
    }
    //批量删除
    @RequestMapping("/batchDeleteBook")
    public Integer batchDeleteBook(@RequestParam List<Integer> ids){
        Integer batch=bookInfoService.batchDelete(ids);
        return batch;
    }
}
